/**
 * 
 */
package kr.co.insoft.publicmesh.controller.configration;

import java.util.List;

import kr.co.insoft.core.model.configration.connection.ConnectionEntityModel;
import kr.co.insoft.core.model.configration.vender.VenderEntityModel;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.service.configration.connection.ConnectionService;
import kr.co.insoft.core.service.configration.vender.VenderService;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author skpark
 * 
 */
@Controller
@RequestMapping(value = "configration/connection")
public class ConnectionController {

	private static final Logger logger = LoggerFactory
			.getLogger(ConnectionController.class);

	final String TILES_PAGE_PREFIX = "tiles/configration/connection";
	final String SINGLE_PAGE_PREFIX = "configration/connection";

	@Autowired
	ConnectionService connectionService;

	@Autowired
	VenderService venderService;

	/**
	 * Connection 목록
	 * 
	 * @param model
	 * @param pagingModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "lists")
	public String lists(
			Model model,
			@ModelAttribute("pagingModel") PagingModel pagingModel,
			@RequestParam(value = "venderid", required = false) String venderid,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {

		List<VenderEntityModel> combolist = venderService.getComboList();

		// vender 목록이 없을 경우는 vender부터 우선 등록해야함.
		if (combolist == null || combolist.size() < 1) {
			model.addAttribute("message", "common.configration.vender.empty");

			if (ip.equals("Y"))
				return SINGLE_PAGE_PREFIX + "/message";

			return TILES_PAGE_PREFIX + "/message";
		}

		if (!org.springframework.util.StringUtils.hasLength(venderid)
				&& (combolist != null && combolist.size() > 0)) {
			VenderEntityModel venderEntityModel = combolist.get(0);
			venderid = venderEntityModel.getVenderid();
		}

		pagingModel.getSearchAndCondition().put("venderid", venderid);

		model.addAttribute("vender", venderService.getArticle(venderid));
		model.addAttribute("venderComboList", combolist);
		model.addAttribute("pagingModel",
				connectionService.getLists(pagingModel));

		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/lists";

		return TILES_PAGE_PREFIX + "/lists";
	}

	/**
	 * Connection 등록 폼 호출
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.GET)
	public String createForm(
			Model model,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {

		List<VenderEntityModel> venderList = venderService.getComboList();
		model.addAttribute("venderList", venderList);
		model.addAttribute(new ConnectionEntityModel());
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/createForm";

		return TILES_PAGE_PREFIX + "/createForm";
	}

	/**
	 * Connection 등록
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.POST)
	public String createPool(
			@ModelAttribute("connectionEntityModel") ConnectionEntityModel connectionEntityModel,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip,
			final BindingResult bindingResult) throws Exception {

		if (bindingResult.hasErrors()) {
			if (ip.equals("Y"))
				return SINGLE_PAGE_PREFIX + "/createForm";

			return TILES_PAGE_PREFIX + "/createForm";
		}

		connectionService.createConnection(connectionEntityModel);

		if (ip.equals("Y"))
			return "redirect:./lists?ip=Y";

		return "redirect:./lists";
	}

	/**
	 * Connection 상세
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "view", method = RequestMethod.GET)
	public String view(
			Model model,
			@RequestParam("connectionid") String connectionid,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {

		List<VenderEntityModel> venderList = venderService.getComboList();
		model.addAttribute("venderList", venderList);
		model.addAttribute(connectionService.getArticle(connectionid));

		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/view";

		return TILES_PAGE_PREFIX + "/view";
	}

	/**
	 * Connection 삭제
	 * 
	 * @param connectionEntityModel
	 * @return
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST)
	public @ResponseBody
	ResponseEntity<Boolean> remove(
			@ModelAttribute("connectionEntityModel") ConnectionEntityModel connectionEntityModel) {
		try {
			connectionService.removeConnection(connectionEntityModel);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
}
